package com.xhtt.modules.cfg.schedule;

import com.xhtt.common.utils.ListUtils;
import com.xhtt.common.utils.SpringContextUtils;
import com.xhtt.modules.cfg.entity.CfgTubeInfo;
import com.xhtt.modules.cfg.entity.CfgTubeLog;
import com.xhtt.modules.cfg.entity.CfgTubeProduct;
import com.xhtt.modules.cfg.service.CfgTubeInfoService;
import com.xhtt.modules.cfg.service.CfgTubeLogService;
import com.xhtt.modules.cfg.service.CfgTubeProductService;
import com.xhtt.modules.cfg.service.impl.CfgTubeInfoServiceImpl;
import com.xhtt.modules.cfg.service.impl.CfgTubeLogServiceImpl;
import com.xhtt.modules.cfg.service.impl.CfgTubeProductServiceImpl;
import lombok.Data;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Data
public class TubeLog implements Runnable {
    private CfgTubeInfoService tubeInfoService = SpringContextUtils.getBean(CfgTubeInfoServiceImpl.class);
    private CfgTubeProductService tubeProductService = SpringContextUtils.getBean(CfgTubeProductServiceImpl.class);
    private CfgTubeLogService tubeLogService = SpringContextUtils.getBean(CfgTubeLogServiceImpl.class);

    private CfgTubeInfo current;
    private CfgTubeInfo old;
    private List<CfgTubeProduct> oldProductList;
    private String deptUser;

    private TubeLog() {
    }

    public TubeLog(CfgTubeInfo current, CfgTubeInfo old, List<CfgTubeProduct> oldProductList, String deptUser) {
        this.current = current;
        this.old = old;
        this.oldProductList = CollectionUtils.isEmpty(oldProductList) ? new ArrayList<>() : oldProductList;
        this.deptUser = deptUser;
    }

    @Override
    public void run() {
        try {
            String rfidInfo = "";
            String comProductInfo = "";
            String spareProductInfo = "";

            if (!ObjectUtils.nullSafeEquals(current.getRfid(), old.getRfid())) {
                rfidInfo = old.getRfid() + " → " + current.getRfid();
            }

            CfgTubeProduct curComProduct = null == current.getComTubeProduct() ? new CfgTubeProduct() : current.getComTubeProduct();
            CfgTubeProduct oldComProduct = oldProductList.stream().filter(p -> p.getType().intValue() == 1).findFirst().orElse(new CfgTubeProduct());
            if (!ObjectUtils.nullSafeEquals(curComProduct.getProductId(), oldComProduct.getProductId())) {
                comProductInfo = (null == oldComProduct.getName() ? "" : oldComProduct.getName()) + " → " + (null == curComProduct.getName() ? "" : curComProduct.getName());
            }

            List<CfgTubeProduct> curSpareProductList = null == current.getSpareTubeProducts() ? new ArrayList<>() : current.getSpareTubeProducts();
            List<String> curSpareProductNames = curSpareProductList.stream().map(p -> p.getName()).collect(Collectors.toList());
            oldProductList.removeIf(p -> p.getType().intValue() == 1);
            List<String> oldSpareProductNames = oldProductList.stream().map(p -> p.getName()).collect(Collectors.toList());
            if (!ListUtils.listEqualsCheck(curSpareProductNames, oldSpareProductNames)) {
                spareProductInfo = String.join(",", oldSpareProductNames) + " → " + String.join(",", curSpareProductNames);
            }

            if (StringUtils.isNotEmpty(rfidInfo) || StringUtils.isNotEmpty(comProductInfo) || StringUtils.isNotEmpty(spareProductInfo)) {
                CfgTubeLog log = new CfgTubeLog();
                log.setTubeId(current.getId());
                log.setDeptUser(deptUser);
                log.setRfidInfo(rfidInfo);
                log.setComProductInfo(comProductInfo);
                log.setSpareProductInfo(spareProductInfo);
                tubeLogService.save(log);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
